{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<h3>文本特征抽取</h3>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<h3>文本特征抽取</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data_new:\n",
      " [[0 1 1 2 0 1 1 0]\n",
      " [1 1 1 0 1 1 0 1]]\n",
      "特征名字:\n",
      " ['dislike', 'is', 'life', 'like', 'long', 'python', 'short', 'too']\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "\"\"\"英文文本抽取\"\"\"\n",
    "data = [\"life is short , i like like python\",\n",
    "    \"life is too long , i dislike python\"]\n",
    "\n",
    "# 实例化一个转换器类\n",
    "transfer = CountVectorizer()\n",
    "\"\"\" 统计每个样本特征词出现的个数\"\"\"\n",
    "# 调用fit_tranform\n",
    "data_new = transfer.fit_transform( data )\n",
    "print( \"data_new:\\n\",data_new.toarray() )\n",
    "print(\"特征名字:\\n\",transfer.get_feature_names())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data_new:\n",
      " [[0 1]\n",
      " [1 0]]\n",
      "特征名字:\n",
      " ['天安门上太阳升', '我爱北京天安门']\n"
     ]
    }
   ],
   "source": [
    "\"\"\"中文文本抽取\"\"\"\n",
    "data = [\"我 爱 北京 天安门\",\"天安门 上 太阳升\"]\n",
    "\n",
    "# 实例化一个转换器类\n",
    "transfer = CountVectorizer()\n",
    "\"\"\" 统计每个样本特征词出现的个数\"\"\"\n",
    "# 调用fit_tranform\n",
    "data_new = transfer.fit_transform( data )\n",
    "print( \"data_new:\\n\",data_new.toarray() )\n",
    "print(\"特征名字:\\n\",transfer.get_feature_names())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
